package com.wenwei.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wenwei.adapter.MessageAdapter;
import com.wenwei.bean.MessageListBean;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.UserStringUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/22.
 */
public class MessageActivity extends BaseActivity implements MessageAdapter.CallBack {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.recycler)
    XRecyclerView recycler;
    @Bind(R.id.empty)
    View mView;
    private MessageAdapter mAdapter;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("消息中心");

        recycler.setEmptyView(mView);
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                recycler.setLoadingMoreEnabled(true);
                getMessageList(1);
            }

            @Override
            public void onLoadMore() {
                if (currentPage < pageTotalCount) {
                    currentPage = currentPage + 1;
                    recycler.setLoadingMoreEnabled(true);
                    getMessageList(2);
                } else {
                    recycler.setLoadingMoreEnabled(false);
                    recycler.loadMoreComplete();
                }
            }
        });
        mAdapter = new MessageAdapter();
        mAdapter.setmCallBack(this);
        recycler.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getMessageList(1);
    }

    private BaseSubscriber<MessageListBean> messageSubscriber;
    private int currentPage = 1;//当前分页
    private int pageTotalCount;//分页总数
    private ArrayList<MessageListBean.DataBean> mCommonList = new ArrayList<>();
    private void getMessageList(final int type){
        messageSubscriber = new BaseSubscriber<MessageListBean>(mContext,true,"加载中...") {
            @Override
            public void onSuccess(MessageListBean messageListBeen) {


                pageTotalCount = messageListBeen.getTotal_page();

                if(type == Constant.LOAD_TYPE_REFRESH){
                    mCommonList.clear();
                    mCommonList.addAll(messageListBeen.getData());
                    mAdapter.setDataList(mCommonList);
                    recycler.refreshComplete();
                }else{
                    mCommonList.addAll(messageListBeen.getData());
                    mAdapter.setDataList(mCommonList);
                    recycler.loadMoreComplete();
                }

                mAdapter.notifyDataSetChanged();
            }
        };
        ApiManager.getInstance().getMessageList(messageSubscriber, UserStringUtils.getUserId(mContext),currentPage);
    }

    @Override
    public void onItemClick(MessageListBean.DataBean bean) {
        Bundle b = new Bundle();
        b.putSerializable("bean",bean);
        goActivity(mContext,MessageDetailsActivity.class,b);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick(R.id.base_back)
    public void onViewClicked() {
        finish();
    }
}
